//
//  HXLoginViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/19.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXLoginViewController.h"
#import "HXRegisterViewController.h"
#import "HX_Macro.h"
#import "MineViewController.h"
#import "NSString+HXMD5.h"
#import "SVProgressHUD.h"
@interface HXLoginViewController ()

@end

@implementation HXLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.backBtn addTarget:self action:@selector(backBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginBtn addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.btnYes addTarget:self action:@selector(addYes:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)addYes:(UIButton *)btn {
    [self getLoginNetWorking];
}
- (void)backBtn:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
- (void)getLoginNetWorking {
    NSString *st = self.textPassField.text;
    NSString *finalString = [st stringWithhx_MD5_32Bitmd_5Lower];
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"device_id=100d8559094fe4cba38&device_name=R6007&password=%@&methodName=UserSignin&device_type=android&mobile=%@&version=4.40&", finalString,self.textTelePhone.text];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        if ([results[@"msg"] isEqualToString:@"success"]) {
            MineViewController *min = [[MineViewController alloc] init];
            min.modalTransitionStyle = 2;
            [self presentViewController:min animated:YES completion:^{
            }];
        } else {
            //警告框提示:
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您输入的账号或密码有误是否重新输入" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *actionCancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                MineViewController *min = [[MineViewController alloc] init];
                [self presentViewController:min animated:YES completion:^{
                }];
            }];
            [alertController addAction:actionCancle];
            UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (![self.textTelePhone.text isEqualToString:@"15134489822"]) {
                 self.textTelePhone.text = @"";
                }
                if (![self.textPassField.text isEqualToString:@"123456789"]) {
                    self.textPassField.text = @"";
                }

            }];
            [alertController addAction:actionConfirm];
            [self showDetailViewController:alertController sender:nil];
        }
        
    } Failure:^(NSError *error) {
    }];
   
}
- (void)addBtn:(UIButton *)btn {
    HXRegisterViewController *registe = [[HXRegisterViewController alloc] init];
    registe.modalTransitionStyle = 2;
    [self presentViewController:registe animated:YES completion:^{
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
